#!/usr/bin/env sh

# necessary to load scripting-addition during startup on macOS Big Sur
# *yabai --load-sa* is configured to run through sudo without a password
sudo yabai --load-sa
yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"

# ====
# Initialization
# ====

source ~/.config/yabai/scripts/initialization.sh

refresh_swiftbar_yabai_plugin="open -g 'swiftbar://refreshplugin?name=yabai'"

# ====
# Rules
# ====

# Unmanaged applications
yabai -m rule --add app=".* Preferences" manage=off
yabai -m rule --add app="System Information" manage=off
yabai -m rule --add app="EuDic" manage=off
yabai -m rule --add app="Activity Monitor" manage=off
yabai -m rule --add app="App Store" manage=off
yabai -m rule --add app="1Password .*" manage=off
yabai -m rule --add app="Bartender" manage=off
yabai -m rule --add app="iStat Menu" manage=off
yabai -m rule --add app="Karabiner" manage=off
yabai -m rule --add title="Preferences" manage=off
yabai -m rule --add app="Installer" manage=off
yabai -m rule --add app="Fantastical Helper" manage=off # Fantastical menu bar drop-down menu

# Border on
yabai -m rule --add app="kitty|Alacritty" border=on
yabai -m rule --add app="iTerm2" title!="Preferences|Profiles|Updating|^$" border=on

# ====
# Signals
# ====

# Initialize the temp folder when adding or removing displays
yabai -m signal --add event=display_added action="~/.config/yabai/scripts/initialization.sh"
yabai -m signal --add event=display_removed action="~/.config/yabai/scripts/initialization.sh"

# Refresh yabai SwiftBar plugin
yabai -m signal --add event=space_changed action="${refresh_swiftbar_yabai_plugin}"
yabai -m signal --add event=display_added action="${refresh_swiftbar_yabai_plugin}"
yabai -m signal --add event=display_removed action="${refresh_swiftbar_yabai_plugin}"
yabai -m signal --add event=display_changed action="${refresh_swiftbar_yabai_plugin}"
yabai -m signal --add event=display_moved action="${refresh_swiftbar_yabai_plugin}"
yabai -m signal --add event=display_moved action="${refresh_swiftbar_yabai_plugin}"

# ====
# Configs
# ====

# General
yabai -m config layout                       bsp
yabai -m config split_ratio                  0.50
yabai -m config auto_balance                 off
yabai -m config insert_feedback_color        0xaad75f5f

# Window
yabai -m config window_placement             second_child
yabai -m config window_topmost               off
yabai -m config window_shadow                off
yabai -m config window_opacity               off
yabai -m config window_opacity_duration      0.0
yabai -m config active_window_opacity        1.0
yabai -m config normal_window_opacity        0.90

# Window border
yabai -m config window_border off
yabai -m config window_border_width 3
yabai -m config active_window_border_color 0xffffbb7d
yabai -m config normal_window_border_color 0xff353c54

# Mouse
yabai -m config mouse_follows_focus          off
yabai -m config focus_follows_mouse          off
yabai -m config mouse_modifier               fn
yabai -m config mouse_action1                move
yabai -m config mouse_action2                resize

# Paddings
yabai -m config top_padding                  5
yabai -m config bottom_padding               5
yabai -m config left_padding                 5
yabai -m config right_padding                5
yabai -m config window_gap                   5
# yabai -m config external_bar                 all:28:0

# Refresh yabai SwfitBar plugin after reboot
open -g 'swiftbar://refreshplugin?name=yabai'

echo "yabai configuration loaded.."
